<?php
function checkQQInput($qq) {
	$qq_str = "/^[1-9][0-9]{4,}$/";
	if(preg_match($qq_str, $qq)) {
		return true;
	}else {
		return false;
	}
}

function isMail($mail)  { 
	$reg_mail = "/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/";
	if(preg_match($reg_mail, $mail)){
		return true;
	}
    else
    {
    	return false;
    }
}

function checkCardIDInput($cardId) {
    $reg_str18 = "/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}[0-9Xx]$/";
    $reg_str15 = "/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/";
    
    if(preg_match($reg_str18, $cardId) || preg_match($reg_str15, $cardId)) {
        return true;
    }
    else {
        return false;
    }
}

function check_birthday_of_card_id($card_id)
{
	$idcard_length=strlen($card_id);
	if($idcard_length===18)
	{
		$year=substr($card_id,6,4);
		$month=substr($card_id,10,2);
		$day=substr($card_id,12,2);
		$birthday=substr($card_id,6,8);
	}
	else if($idcard_length===15)
	{
		$year='19'.substr($card_id,6,2);
		$month=substr($card_id,8,2);
		$day=substr($card_id,10,2);
		$birthday='19'.substr($card_id,6,6);
	}
	else 
	{
		return false;
	}
	if($month<1 || $month>12 || $day<1 || $day>31 || $birthday > date('Ymd',time())  ||  $birthday < 19000101)return false;
	else return array('year'=>$year,'month'=>$month,'day'=>$day);			
}

//检验身份证 省份码
function check_cardIDbyarea($card_id)
{
	$areaarr = array('11','12','13','14','15','21','22','23','31','32','33','34','35','36','37','41','42','43','44','45','46','50','51','52','53','54','61','62','63','64','65','71','81','82','91');
	$arrea = substr($card_id,0,2);
	if(!in_array($arrea,$areaarr))
	{
		return false;
	}
	else
	{
		return true;
	}
}

if(!function_exists('get_client_ip_from_ns')){
load_library('pub:core/client_ip', false);
//	function get_client_ip_from_ns($proxy_override = false) 
//	{	
//		if ($proxy_override) {
//			/* 优先从代理那获取地址或者 HTTP_CLIENT_IP 没有值 */
//			$ip = empty($_SERVER["HTTP_X_FORWARDED_FOR"]) ? (empty($_SERVER["HTTP_CLIENT_IP"]) ? NULL : $_SERVER["HTTP_CLIENT_IP"]) : $_SERVER["HTTP_X_FORWARDED_FOR"];
//		} else {
//			/* 取 HTTP_CLIENT_IP, 虽然这个值可以被伪造, 但被伪造之后 NS 会把客户端真实的 IP 附加在后面 */
//			$ip = empty($_SERVER["HTTP_CLIENT_IP"]) ? NULL : $_SERVER["HTTP_CLIENT_IP"];
//		}
//
//		if (empty($ip)) {
//			$ip = $_SERVER['REMOTE_ADDR'];
//		}
//
//		/* 真实的IP在以逗号分隔的最后一个, 当然如果没用代理, 没伪造IP, 就没有逗号分离的IP */
//		if ($p = strrpos($ip, ",")) {
//			$ip = substr($ip, $p+1);
//		}
//		return trim($ip);
//	}
}

function validation_filter_id_card($id_card)
{
	if(strlen($id_card) == 18)
	{
		return idcard_checksum18($id_card);
	}
	elseif((strlen($id_card) == 15))
	{
		$id_card = idcard_15to18($id_card);
		return idcard_checksum18($id_card); 
	}
	else 
	{
		return false;
	}
}
 
// 计算身份证校验码，根据国家标准GB 11643-1999 
function idcard_verify_number($idcard_base)
{ 
	if(strlen($idcard_base) != 17)
	{
		return false;
	}
	//加权因子
	$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 
	
	//校验码对应值
	$verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); 
	$checksum = 0; 
	for ($i = 0; $i < strlen($idcard_base); $i++)
	{
		$checksum += substr($idcard_base, $i, 1) * $factor[$i]; 
	}
	$mod = $checksum % 11; 
	$verify_number = $verify_number_list[$mod]; 
	return $verify_number;  
}

// 将15位身份证升级到18位 
function idcard_15to18($idcard){ 
	if (strlen($idcard) != 15){ 
		return false; 
	}else{ 
		// 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码 
		if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false){ 
		$idcard = substr($idcard, 0, 6) . '18'. substr($idcard, 6, 9); 
		}else{ 
		$idcard = substr($idcard, 0, 6) . '19'. substr($idcard, 6, 9);
		} 
	} 
	$idcard = $idcard . idcard_verify_number($idcard); 
	return $idcard; 
} 

// 18位身份证校验码有效性检查 
function idcard_checksum18($idcard){ 
	if (strlen($idcard) != 18){ return false; } 
	$idcard_base = substr($idcard, 0, 17); 
	if (idcard_verify_number($idcard_base) != strtoupper(substr($idcard, 17, 1))){ 
		return false; 
	}else{ 
		return true; 
	} 
}
?>
